/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import entity.User;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

/**
 *
 * @author Wink
 */
@Stateless
@Path("users")
public class UsersFacadeREST extends AbstractFacade<User> {
    @PersistenceContext(unitName = "HashArea-warPU")
    private EntityManager em;

    public UsersFacadeREST() {
        super(User.class);
    }

    @POST
    @Produces({"application/json", "application/xml"})
    public Response create(@QueryParam("name") String name) {
        System.out.println("Creating user " + name);
        if(super.find(name) == null) {
            User newUser = new User(name);
            super.create(newUser);
            return Response.ok(newUser).build();
        } else {
            return Response.status(Response.Status.CONFLICT).build();
        } 
    }
    
//    @DELETE
//    @Path("{id}")
//    public void remove(@PathParam("id") String id) {
//        super.remove(super.find(id));
//    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
} 